/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/spf13/cobra"
)

// showCmd represents the show command
func show(url string, cmd *cobra.Command, args []string) {
	// fmt.Println("show called")
	client := http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("创建请求失败: %v\n", err)
		return
	}

	q := req.URL.Query()
	name, _ := cmd.Flags().GetString("name")
	q.Add("name", name)
	req.URL.RawQuery = q.Encode()

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("请求失败: %v\n", err)
		return
	}
	defer resp.Body.Close()
	buf, _ := ioutil.ReadAll(resp.Body)
	fmt.Print(string(buf))
}

var service_showCmd = &cobra.Command{
	Use: "show",
	Run: func(cmd *cobra.Command, args []string) {
		show(cmd_url_prefix+"/api/service/show", cmd, args)
	},
}

var public_showCmd = &cobra.Command{
	Use: "show",
	Run: func(cmd *cobra.Command, args []string) {
		show(cmd_url_prefix+"/api/public/show", cmd, args)
	},
}

var node_showCmd = &cobra.Command{
	Use: "show",
	Run: func(cmd *cobra.Command, args []string) {
		show(cmd_url_prefix+"/api/node/show", cmd, args)
	},
}

func init() {
	serviceCmd.AddCommand(service_showCmd)
	publicCmd.AddCommand(public_showCmd)
	nodeCmd.AddCommand(node_showCmd)

	service_showCmd.Flags().String("name", "", "service name")
	public_showCmd.Flags().String("name", "", "public name")

	// service_showCmd.MarkFlagRequired("name")
	// public_showCmd.MarkFlagRequired("name")
}
